package com.fantasy.robot;

import java.awt.event.KeyEvent;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;

import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.poifs.filesystem.POIFSFileSystem;
import org.apache.poi.ss.usermodel.Cell;

import com.fantasy.excel.ReadExcel;
import com.fantasy.file.GetFolderAbsPath;
import com.fantasy.readproperties.ReadProperties;

public class ReadExcelToRobot {
	
	private static int rowSpan = Integer.valueOf(ReadProperties.getProperty("robotConfig.properties", "rowSpan"));
	private static int colSpan = Integer.valueOf(ReadProperties.getProperty("robotConfig.properties", "colSpan"));
	private static String colEnter = ReadProperties.getProperty("robotConfig.properties", "colEnter");

	public static void main(String[] args) {
		int startSpan = Integer.valueOf(ReadProperties.getProperty("robotConfig.properties","startSpan"));
		int keyPressSpan = Integer.valueOf(ReadProperties.getProperty("robotConfig.properties","keyPressSpan"));
		int keyDownSpan = Integer.valueOf(ReadProperties.getProperty("robotConfig.properties","keyDownSpan"));
		TestRobot.openRobot(keyPressSpan , keyDownSpan);
		TestRobot.delay(startSpan);
		String startRowNum = ReadProperties.getProperty("robotConfig.properties","startRow");
		String robotFile = ReadProperties.getProperty("robotConfig.properties", "robotFile");
		robot(GetFolderAbsPath.getFilePath(robotFile) , startRowNum);
		// testAddKeyWord(okm , "60aab0d1-324c-492e-8d28-2318e1731533");
	}

	public static void robot(String fileName , String startRowNum) {

		File file = null;
		file = new File(fileName);
		InputStream rptXlsInput;
		try {
			rptXlsInput = new FileInputStream(file);

			POIFSFileSystem fs = new POIFSFileSystem(rptXlsInput);

			HSSFWorkbook wb = new HSSFWorkbook(fs);

			HSSFSheet sheet = wb.getSheetAt(0);

			// 得到总行数
			int rowNum = sheet.getLastRowNum();
			HSSFRow row = sheet.getRow(7);
			// System.out.println("=============-------------------------==============");
			for (int i = Integer.valueOf(startRowNum).intValue(); i <= rowNum; i++) {
				row = sheet.getRow(i);
				for(Cell cell:row){
					TestRobot.delay(colSpan);
					TestRobot.press(ReadExcel.getCellFormatValue(cell));
					System.out.println(ReadExcel.getCellFormatValue(cell));
					if(cell.getColumnIndex() + 1 != row.getLastCellNum()){
						if("true".equals(colEnter) || "Y".equals(colEnter) || "Yes".equals(colEnter)){
							TestRobot.keyDown(KeyEvent.VK_ENTER);
						}
					}else{
					}
					TestRobot.delay(rowSpan);
				}
			}
			wb.close();
			fs.close();
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}

	}

}
